<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;
use app\BaseController;
use think\facade\View;
use app\service\DbHelper;

//管理员
class Admin extends BaseController
{
    protected $table='admin';

    public function index()
    {
        $p=input();
        $where = array();
        $where[]=['id','>',1];
        if($this->admin['id']>1)
        {
            $where[]=['danwei_id','=',$this->admin['danwei_id']];
        }
        if(!empty($p['danwei_id']))
        {
            $where[]=['danwei_id','=',''.$p['danwei_id'].''];
        }
        if(!empty($p['keyword']))
        {
            $where[]=['username','like','%'.$p['keyword'].'%'];
        }
        if(!empty($p['is_master']))
        {
            $where[]=['company_admin','=',1];
        }
        $datalist = Db::name($this->table)->where($where)->paginate(['list_rows'=>10,'query'=>$p]);
        $list=array();
        foreach($datalist as &$v)
        {
            $v['danwei']=Db::name('danwei')->where('id','=',$v['danwei_id'])->find();
            $role_name=Db::name('role')->where('id','in',explode(',',$v['role_id']))->column('title');
            $v['role_name']=implode(',',$role_name);
            $v['add_time']=formatDate($v['add_time']);
            $v['login_time']=formatDate($v['login_time']);
            $list[]=$v;
        }
        $page = $datalist->render();
        View::assign('data_list',$list);
        View::assign('page',$page);
        View::assign('user',$this->admin);
        View::assign('danwei',DbHelper::query(['table'=>'danwei']));
        return View::fetch();
    }
    public function modify()
    {
        $p=input();
        $data=array();
        if(isset($p['id']))
        {
            $data = Db::name($this->table)->where(['id'=>intval($p['id'])])->find();
            $role_id=array();
            $data['role_name']='';
            if(!empty($data['role_id']))
            {
                $role_name=Db::name('role')->where('id','in',json_decode($data['role_id'],true))->column('title');
                $data['role_name']=implode(',',$role_name);
                $data['role_id']=explode(',',$data['role_id']);
            }
        }
        View::assign('data',$data);
        View::assign('admin',$this->admin);
        $role_where=[['status','=',1]];
        if($this->admin['id']>1)
        {
             $child_role_id=Db::name('role')->where('danwei_id',$this->admin['danwei_id'])->column('id');
             foreach (explode(',',$this->admin['role_id']) as $v)
             {
                 array_push($child_role_id,intval($v));
             }
             $role_where[]=['id','in',$child_role_id];
        }
      
        $roles=Db::name('role')->where($role_where)->select();
        View::assign('authorizes',$roles);
        $where_danwei=array();
        if($this->admin['id']>1)
        {
            $where_danwei[]=['id','=',$this->admin['danwei_id']];
        }
        $danwei=Db::name('danwei')->where($where_danwei)->select()->toarray();
        View::assign('danwei',$danwei);
        $county=array();
        if($danwei)
        {
            $county=Db::name('county')->where('city_id',$danwei[0]['id'])->select()->toarray();
        }
        View::assign('county',$county);
        return View::fetch();
    }
    public function save()
    {
        $p=input();
        
        if(empty($p['status']))
        {
            $p['status'] = 0;
        }
        $p['role_id']=empty($p['role_id'])?'':$p['role_id'];
        if(!empty($p['role_id']))
        {
            $role_id=array();
            foreach ($p['role_id'] as $k=>$r)
            {   
                array_push($role_id,$r);
            }
            $p['role_id']=implode(',',$role_id);
        }       
        if(!empty($p['id']))
        {
            $id = $p['id'];
            unset($p['id']);
            
            if(empty($p['login_pwd']))
            {
                unset($p['login_pwd']);
            }
            else
            {
                $p['login_pwd']=md5($p['login_pwd']);
            }           
            if($p['danwei_id']!=$this->admin['danwei_id'])
            {
                 $token=$this->admin['token'];
                 $user=Db::name('admin')->where('id',$id)->find();
                 cache('admin_token_login_'.$token,$user);
                 cookie('admin_token_login',$token);
                 cookie('admin_token_login_key',md5($token.config('app.md5key')));
            }
            $res = Db::name($this->table)->where(['id'=>$id])->update($p);
        }
        else{
            $p['login_pwd']=md5($p['login_pwd']);
            if($this->admin['id']!=1)
            {              
                $p['danwei_id']=$this->admin['danwei_id'];
                $p['city_id']=$this->admin['city_id'];
            }
            $res = Db::name($this->table)->insert($p);
        }
        return jsonReturn($res,'',$p);
    }
    public function status()
    {
        $p = input();
        $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update(['status'=>intval($p['status'])]);
        return jsonRes($res);
    }
    public function remove()
    {
        $p = input();
        if(!is_array($p['id']))
        {
            $p['id']=explode(',',$p['id']);
        }
        $res = Db::name($this->table)->where([['id','in',$p['id']],['id','>',1]])->delete();
        return jsonRes($res);
    }
    public function password()
    {
        $p=input();
        if(isset($p['login_pwd']))
        {
            if($p['login_pwd']!=$p['login_pwd2'])
            {
                return jsonReturn(0,'密码不一致');
            }
            $login_pwd=md5($p['login_pwd']);
            $res = Db::name($this->table)->where(['id'=>$this->admin['id']])->update(['login_pwd'=>$login_pwd]);
            jsonRes($res);
            jsonReturn(1,$res,['p'=>$p,'login_pwd'=>$login_pwd]);
        }
        return View::fetch();
    }
}